package com.qfedu.controller;

import com.qfedu.model.Subitem;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.SubitemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author LiJH
 * @date 2023-02-10
 */
public class SubitemController {
    @Autowired
    private SubitemService subitemService;

    @PostMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = subitemService.findPage(queryPageBean);
        return page;
    }
    @PostMapping("addSubitem")
    public Result addSubitem(@RequestBody Subitem subitem){
        int i = subitemService.addSubitem(subitem);
        Result result = new Result();
        if (i>0){
            result.setFlag(true);
            result.setMessage("添加成功！");
        }else {
            result.setFlag(false);
            result.setMessage("添加失败！");
        }
        return result;
    }
    @GetMapping("findById")
    public Result findById( String suid){
        Subitem byId = subitemService.findById(suid);
        //查询成功
        if (ObjectUtils.notEqual(byId, null)) {
            return Result.successResult("查询成功",byId);
        } else {
            return Result.failResult("查询失败");
        }
    }

    @PostMapping("editSubitem")
    public Result editSubitem(@RequestBody Subitem subitem){
        Result result = new Result();
        int i = subitemService.editSubitem(subitem);
        if (i>0){
            result.setFlag(true);
            result.setMessage("更新成功！");
        }else {
            result.setFlag(false);
            result.setMessage("更新失败！");
        }
        return result;
    }

    @GetMapping("deleteBySuid")
    public Result deleteBySuid(String suid ){
        int i = subitemService.deleteBySuid(suid);
        Result result = new Result();
        if (i>0){
            result.setFlag(true);
            result.setMessage("删除成功！");
        }else {
            result.setFlag(false);
            result.setMessage("删除失败！");
        }
        return result;
    }
}